package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.dao.impl.player.UserHistoryAttributeDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.statistics.AbilityStatistics;
import com.yanqu.road.logic.bussiness.player.UserHistoryAttributeBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.statistics.StatisticsMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import io.netty.channel.ChannelHandlerContext;

import java.util.*;

@Cmd(code = GameProtocol.S_GHOST_NOTIFY_UPLOAD_ABILITY, desc = "通知区服上传门客总能力")
public class GhostNotifyUploadAbilityFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        GhostActivityProto.NotifyUploadAbilityReqMsg reqMsg = GhostActivityProto.NotifyUploadAbilityReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);

        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            return;
        }

        GhostActivityProto.UploadAbilityReqMsg.Builder respMsg = GhostActivityProto.UploadAbilityReqMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setServerId(GameServer.getInstance().getServerId());

        Map<Long, AbilityStatistics> abilityStatisticsMap = StatisticsMgr.getAbilityStatisticsMap();
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp(activityInfo.getBeginShowTime() * 1000);
        for (int i = 0; i < GameConfig.GHOST_GET_ABILITY_DAY_COUNT; i++) {
            zeroTime -= DateHelper.DAY_MILLIONS;
            if (abilityStatisticsMap.containsKey(zeroTime)) {
                // 没有数据的不加进去了，跨服按实际有数据的天数处理
                respMsg.addAbilityList(abilityStatisticsMap.get(zeroTime).getAbility());
            }
        }

        //6.5.0，上传玩家门客历史总赚钱，用户BOSS血量计算
        Map<Long, UserHistoryAttribute> userHistoryAttributeMap = this.getUserHistoryAbilityMap();
        for (UserHistoryAttribute userHistoryAttribute : userHistoryAttributeMap.values()) {
            GhostActivityProto.UserAbilityItem.Builder userAbilityItem = GhostActivityProto.UserAbilityItem.newBuilder();
            userAbilityItem.setUserId(userHistoryAttribute.getUserId());
            userAbilityItem.setUserAbility(userHistoryAttribute.getAbility().longValue());
            respMsg.addUserAbilityList(userAbilityItem);
        }

        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(CrossProtocol.C2_GHOST_ACTIVITY_UPLOAD_ABILITY, respMsg));
    }

    /**
     * 获取用户历史能力映射表
     * @return
     */
    private Map<Long, UserHistoryAttribute> getUserHistoryAbilityMap() {
        List<Long> actUserIdList = new ArrayList<>();
        //获取3天活跃玩家
        List<UserInfo> userList = UserMgr.getUserInfoList();
        for ( UserInfo userInfo : userList) {
            //获取3天活跃玩家
            if (DateHelper.calc2DateTDOADays(new Date(userInfo.getLastLoginTime() * 1000), new Date()) <= 3) {
                actUserIdList.add(userInfo.getUserId());
            }
        }
        if (actUserIdList.isEmpty()) {
            return new HashMap<>();
        }
        //查询历史最高门客赚钱
        return UserHistoryAttributeBussiness.getUserHistoryAttributeMap(actUserIdList);
    }
}
